package com.example.myapplication2003;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.myapplication2003.bean.Member;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FragmentTestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);

        //===============网络请求================
        OkHttpClient client = new OkHttpClient();     // 初始化client对象
        // 配置参数
        Request request = new Request.Builder()
                .url("http://129.28.159.96:7001/v1/test/getMembers")
                .build();
        // 发起请求
        client.newCall(request).enqueue(new Callback() {
            // 失败
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                //错误处理
                Log.e("tag", e.toString());
            }

            // 成功
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                // 获取返回数据
                String data = response.body().string();   // 取出数据
                Log.i("tag", data);
                // 通过gson把字符串转成对象
                Gson gson = new Gson();
                ArrayList<Member> dataList = gson.fromJson(data, new TypeToken<    ArrayList<Member>     >(){}.getType());
                // 循环遍历
                for(Member member : dataList) {
                    Log.i("tag", member.getUsername());
                    Log.i("tag", member.getAge() + "");
                }
            }
        });
        //=========================================

        Button button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转activity
                Intent intent =
                        new Intent(FragmentTestActivity.this, BActivity.class);

                // 传递数据
                intent.putExtra("name", "aaa");
                intent.putExtra("age", 20);

                // startActivity(intent);  // 普通跳转
                startActivityForResult(intent, 10);
            }
        });
    }


    // 回调，当从Activity B 返回时会被调用
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 执行逻辑
        if (requestCode == 10 && resultCode == 100) {
            String title = data.getStringExtra("title");
            int num = data.getIntExtra("num", 0);
            Log.i("tag", title);
            Log.i("tag", num +"");
        }
    }
}